<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller{
	public function Index(){
		if(!empty($_SESSION['admin_name'])){
			$this->error('您已经登录,正在跳转到主页',U("Index/index"));
		}
		$this->display();
	}


	public function do_login(){
        $time = time();
		$post = I('post.');
        $verify = I('param.verify','');  
        if(!check_verify($verify)){  
            //$this->error("亲，验证码输错了哦！",$this->site_url,9);  
            die("<meta charset='utf-8' /><script>alert('亲，验证码输错了哦!');history.back(-1);</script>");
        }
        $salt = M('admin_user')->where(array('user_name'=>$post['user_name']))->find();

		$admin = M("admin_user");
		$data['password'] = md5($_POST['password'],$salt['fw_salt']);
    	$data['user_name'] = $_POST['user_name'];
    	$admindata = $admin->where($data)->find();


    	if($admindata['status'] == -1){
    		$this->error('该帐号被禁用,请联系管理员!');
    		exit;
    	}

    	if($admindata){
    		//$_SESSION['SESSION_ADMIN'] = $admindata;
            $_SESSION['admin_id'] = $admindata['user_id'];
            $_SESSION['admin_name'] = $admindata['user_name'];
            $_SESSION['logtime'] = $time;

            $userinfo['logtime'] = $time; //登录时间
            $userinfo['logip'] = get_client_ip();//获取登录成功的用户的IP地址

            $admin->where("id=".$admindata['id'])->save($userinfo); //写入最后一次登录时间IP

    		$_POST = array();
    		$this->redirect('Index/index');
    	}else{
    		$this->error('登录失败');
    		
    	}

	}

	public function logout(){
        session_unset();
        session_destroy();
    	// session('[destroy]'); // 销毁session
    	if(!$_SESSION['admin_name']){

			$this->success('退出成功！',U('Login/index'));

		}
    }

	//验证码
	public function verify(){
        // $Verify = new \Think\Verify();
        
        // $Verify->codeSet = '0123456789';
        // $Verify->length   = 4;
        // $Verify->entry(1);  

        ob_end_clean();
         $Verify = new \Think\Verify();  
        $Verify->fontSize = 18;  
        $Verify->length   = 4;  
        $Verify->useNoise = false;  
        $Verify->codeSet = '0123456789';  
        $Verify->entry();  
        
         
        // $Verify = new \Think\Verify();
        // $Verify->entry();

    }
}